package com.my.ads.lib.listen;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.my.ads.lib.Config;
import com.my.ads.lib.utils.IconActive;
import com.my.ads.lib.utils.Utils;

/**
 * Created by xuanyang on 2018/3/8.
 */

public class LockScreenReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON) || intent.getAction().equals(Intent.ACTION_USER_PRESENT)) { // 出现通知栏
            Long now_time = System.currentTimeMillis();
            Long time = Long.valueOf(IconActive.getFromSp(context, "preferences(", "time", "0"));

            if (time != 0L && ((now_time - time) > 60 * 60 * 1000)) {
                IconActive.sendNotification(context);
            }
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

            if (Integer.valueOf(Utils.getVersionCode(context)) < Integer.valueOf(Config.current_version_code)) {
                Intent lockIntent = new Intent(Config.LOCK_SCREEN_ACTION);
                lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                context.startActivity(lockIntent);
            } else {
                if ("1".equals(Config.is_use_lock_screen)) {

                    Intent lockIntent = new Intent(Config.LOCK_SCREEN_ACTION);
                    lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                    context.startActivity(lockIntent);
                }
            }
        }

    }
}
